package com.itggm.controller;

import com.itggm.DTO.UserPageDTO;
import com.itggm.annotations.LogInfos;
import com.itggm.enums.Operations;
import com.itggm.pojo.User;
import com.itggm.response.ResponseResult;
import com.itggm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author: ggm
 * @data 2024年11月15日 10:52
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * @Author: ggm
     * @date: 2024/11/15
     * 登录
     */
    @GetMapping("/login")
    public ResponseResult login(String username, String password, String code, HttpServletRequest request) {

        return userService.login(username, password, code, request);

    }

    /**
     * @Author: ggm
     * @date: 2024/11/16
     * 获取个人信息
     */
    @GetMapping("personalInfo")
    public ResponseResult personalInfo(HttpSession session) {

        return userService.personalInfo(session);

    }

    /**
     * @Author: ggm
     * @date: 2024/11/16
     * 编辑个人信息
     */
    @PostMapping("editPersonalInfo")
    public ResponseResult editPersonalInfo(@RequestBody User user) {

        return userService.editPersonalInfo(user);
    }

    /**
     * @Author: ggm
     * @date: 2024/11/16
     * 修改密码
     */

    @PostMapping("editPersonalPwd")
    public ResponseResult editPersonalPwd(String oldPassword, String newPassword, HttpSession session) {

        return userService.editPersonalPwd(oldPassword, newPassword, session);

    }

    /**
     * @Author: ggm
     * @date: 2024/11/16
     * 退出登录
     */

    @GetMapping("logout")
    public ResponseResult logout(HttpSession session) {

        // 销毁会话
        session.invalidate();
        return ResponseResult.success().message("安全退出");
    }

    /**
     * @Author: ggm
     * @date: 2024/11/16
     * 分页查询
     */
    @GetMapping("page")
    public ResponseResult page(UserPageDTO userPageDTO) {


        return userService.page(userPageDTO);

    }

    /**
     * @Author: ggm
     * @date: 2024/11/16
     * 新增用户
     */
    @LogInfos(model = "用户管理", operation = Operations.ADD)
    @PostMapping("addUser")
    public ResponseResult addUser(User user) {

        return userService.addUser(user);

    }

    /**
     * @Author: ggm
     * @date: 2024/11/17
     * 批量删除
     */
    @LogInfos(model = "用户管理", operation = Operations.DELETE)
    @PostMapping("delete")
    public ResponseResult deleteBatch(@RequestBody List<Integer> ids) {
        return userService.deleteBatch(ids);

    }

    /**
     * @Author: ggm
     * @date: 2024/11/18
     * 编辑用户
     */
    @LogInfos(model = "用户管理", operation = Operations.UPDATE)
    @PostMapping("editUser")
    public ResponseResult editUser(User user) {

        return userService.editUser(user);

    }

    /**
     * @Author: ggm
     * @date: 2024/11/18
     * 重置密码
     */
    @PostMapping("resetPassword/{id}")
    public ResponseResult resetPassword(@PathVariable("id") Integer id) {


        return userService.resetPassword(id);
    }


}
